package com.dbutils;

import com.dbutils.entity.User;
import com.dbutils.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialClob;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.sql.SQLException;

public class Test {

    public static void main(String[] args) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        Object[] param = new Object[]{1};
        User user = queryRunner.query("select * from user where id =?", new BeanHandler<User>(User.class), param);
        System.out.println(user);
    }

    public void testClob() throws Exception {
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        String sql = "insert into clob values(?)";
        File file = new File("c:/a.txt");
        Long l = file.length();
        char[] buffer = new char[l.intValue()];
        FileReader reader = new FileReader(file);
        reader.read(buffer);
        SerialClob clob = new SerialClob(buffer);
        queryRunner.update(sql, clob);
    }


    public void testBlob() throws Exception{
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        String sql = "insert into blob values(?)";
        File file = new File("c:/a.jpg");
        Long l = file.length();
        byte[] buffer = new byte[l.intValue()];
        FileInputStream input = new FileInputStream(file);
        input.read(buffer);
        SerialBlob blob = new SerialBlob(buffer);
        queryRunner.update(sql,blob);
    }
}


